<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *  Configuration Model Class  
 *  
 * @package XGO CMS v2.0 
 * @subpackage Configuration  
 * @author Manh Nguyen - 08/08/2012 
 * @link http://sunsoft.vn
 */
class Configuration_model extends DataMapper {

    var $model = 'configuration_model';
    var $table = 'configuration';

    // --------------------------------------------------------------------
    // Construct
    // --------------------------------------------------------------------
    public function __construct() {
        parent::__construct();
    }

    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    var $has_one = array();
    var $has_many = array();
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------
    var $validation = array();
    // --------------------------------------------------------------------

    /**
     * Default order in query select 
     */
    var $default_order_by = array('id');

    /**
     * string value of object
     * 
     * @access private
     * @param null
     * @return string 
     */
    function __toString() {
        return empty($this->title) ? '---empty---' : $this->title;
    }

    /**
     * string value of code
     *
     * @access public
     * @param object
     * @return bool
     */
    function get_code() {
        return '<xmp>' . $this->code . '</xmp>';
    }

    /**
     * string value of title
     *
     * @access public
     * @param object
     * @return bool
     */
    function get_title() {
        return '<xmp>' . $this->title . '</xmp>';
    }

    /**
     * string value of value
     *
     * @access public
     * @param object
     * @return bool
     */
    function get_value() {
        return '<xmp>' . $this->value . '</xmp>';
    }
    
    function get_by_code($code) {
    	$this->select('value, code');
    	$this->where_in('code', $code);
        $code_list = new StdClass();
    	foreach($this->get() as $item){
            $code_tmp = $item->code;
            $code_list->$code_tmp = $item->value;
    	}
    	
        //print_r($code_list);exit;
    	return $code_list;
    }

    /**
     * Array metadata for generate jquery grid
     * show data
     * 
     * @access public
     * @param null
     * @return array 
     */
    function jqgrid_metadata() {
        return array(
            'names' => array(
                'id',
                $this->localize_label('title'),
                $this->localize_label('code'),
                $this->lang->line('value'),
                $this->lang->line('description')
            ),
            'model' => array(
                array('name' => 'id', 'hidden' => TRUE, 'key' => TRUE,
                    'editrules' => array('edithidden' => TRUE)
                ),
                array('name' => 'title', 'width' => '100',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'code', 'width' => '100',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'value',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                ),
                array('name' => 'description',
                    'searchoptions' => array(
                        'sopt' => array('cn', 'nc', 'eq', 'bw', 'bn', 'ew', 'en')
                    )
                )
            )
        );
    }
    
    function total(){
    	$this->select("count(id) as total");
    	$this->get();
    	 
    	return array(
    			"title" => $this->lang->line('configuration'),
    			"total"	=> $this->total,
    	);
    }

}

/* End of file configuration.php */
/* Location: ./application/models/configuration.php */